# 日切并且刷新redis缓存（用于T+2对账）

from libs.redis_op import Redis
from datetime import datetime, timedelta
from libs.db2 import OceanDb


def run(days, clear_bank_no='14373020'):
    # days=0 日切为当天 days=1 日切为T+1 以此类推
    settle_date = (datetime.now() + timedelta(days=days)).strftime('%Y-%m-%d')
    back_settle_date = (datetime.now() + timedelta(days=days - 1)).strftime('%Y-%m-%d')
    db = OceanDb()
    try:
        db.execute_sql(
            f"update RTE_PAYCHANNEL_DATE set SETTLE_DATE = '{settle_date}' ,BACK_SETTLE_DATE = '{back_settle_date}' where CLEAR_BANK_NO = '{clear_bank_no}'")
        print('数据库日期修改成功！！！')
        r = Redis()
        r.flushall()
        if days == 0:
            print('系统日切日期已经改为当天！！！')
        elif days == 1:
            print('系统日切日期已经改为T+1！！！')
        elif days == 2:
            print('系统日切日期已经改为T+2！！！')
    except Exception:
        raise


if __name__ == '__main__':
    run(0)
